package Inji;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Modify extends JPanel {

	ArrayList<Data6> list;
	int index;
	boolean check = false;

	private JLabel lblNewLabel;
	private JTextField username;
	private JTextArea textArea;
	private JButton search;
	private JTextField irum;
	private JTextField address;
	private JTextField phone;
	private JTextField email;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	private JLabel lblNewLabel_6;
	private JButton button;
	private JLabel label;
	private JTextField age;
	private JLabel lblModifyDate;

	/**
	 * Create the panel.
	 */
	public Modify() {
		setBackground(new Color(0, 102, 0));
		setLayout(null);
		add(getLblNewLabel());
		add(getUsername());
		add(getTextArea());
		add(getSearch());
		add(getIrum());
		add(getAddress());
		add(getPhone());
		add(getEmail());
		add(getLblNewLabel_2());
		add(getLblNewLabel_4());
		add(getLblNewLabel_5());
		add(getLblNewLabel_6());
		add(getButton());
		add(getLabel());
		add(getAge());
		add(getLblModifyDate());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("이름검색");
			lblNewLabel.setForeground(new Color(255, 255, 255));
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel.setBounds(82, 79, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setBounds(165, 76, 116, 21);
			username.setColumns(10);
		}
		return username;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
			textArea.setBounds(82, 106, 326, 24);
		}
		return textArea;
	}
	private JButton getSearch() {
		if (search == null) {
			search = new JButton("검색");
			search.setBackground(new Color(255, 255, 255));
			search.setFont(new Font("굴림", Font.PLAIN, 11));
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
							
					boolean flag = true;

					if(!username.getText().equals("")){ // 공백이 아니라면
						for(int i=0;i<list.size();i++){
							
							if(username.getText().equals(list.get(i).name)){
								
								flag = false;
								index = i;
								
								SimpleDateFormat sb = new SimpleDateFormat("yy-MM-dd hh:mm");				
								String str = sb.format(list.get(i).nal);
								
								//textArea.append(str+"");
								
								textArea.setText(list.get(i).name +"    "  + list.get(i).address + "    "  + 
										list.get(i).phone +"    "  + list.get(i).email +"    "  + 
										list.get(i).age+"    "  +str);
								break;
							}
						}
					}

					if(flag)
						textArea.setText("검색할 데이터가 존재하지 않습니다.");

					

				}
			});
			search.setBounds(311, 75, 97, 23);
		}
		return search;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setBounds(165, 155, 116, 21);
			irum.setColumns(10);
		}
		return irum;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setBounds(165, 186, 116, 21);
			address.setColumns(10);
		}
		return address;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setBounds(165, 217, 116, 21);
			phone.setColumns(10);
		}
		return phone;
	}
	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setBounds(165, 248, 116, 21);
			email.setColumns(10);
		}
		return email;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("이름");
			lblNewLabel_2.setForeground(new Color(255, 255, 255));
			lblNewLabel_2.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel_2.setBounds(82, 158, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("주소");
			lblNewLabel_4.setForeground(new Color(255, 255, 255));
			lblNewLabel_4.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel_4.setBounds(82, 189, 57, 15);
		}
		return lblNewLabel_4;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("연락처");
			lblNewLabel_5.setForeground(new Color(255, 255, 255));
			lblNewLabel_5.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel_5.setBounds(82, 220, 57, 15);
		}
		return lblNewLabel_5;
	}
	private JLabel getLblNewLabel_6() {
		if (lblNewLabel_6 == null) {
			lblNewLabel_6 = new JLabel("이메일");
			lblNewLabel_6.setForeground(new Color(255, 255, 255));
			lblNewLabel_6.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel_6.setBounds(82, 251, 57, 15);
		}
		return lblNewLabel_6;
	}
	
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.setBackground(new Color(255, 255, 255));
			button.setFont(new Font("굴림", Font.PLAIN, 11));
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
				
						//데이터 입력 여부 체크
						boolean flag = true;  // 모든 값을 입력했을 때
						if(irum.getText().equals("")) flag=false;    // 이름이 공백이면 거짓
						else if(address.getText().equals("")) flag=false; // 주소가 공백이면 거짓
						else if(phone.getText().equals("")) flag=false; // 번호가 공백이면 거짓
						else if(email.getText().equals("")) flag=false;// 메일이 공백이면 거짓
						else if(age.getText().equals("")) flag=false;// 나이가 공백이면 거짓

						if(flag){  // flag가 참이라면
							char number = age.getText().charAt(0); 
							
							if (!(number >= 48 && number <= 57)){
								textArea.setText("나이는 숫자를 입력해야 합니다");
								age.setText("");	
							}
							else{
								
								//Date date = new Date();
								// 입사 날짜는 수정되는게 아니지 않을까? 
								Data6 data = new Data6(irum.getText(), address.getText(), phone.getText(),
										email.getText(), Integer.parseInt(age.getText()), list.get(index).nal);
								
								//data.age = Integer.parseInt(age.getText());
								//d[index] = data;
								
								list.set(index, data);
								
								username.setText("");
								irum.setText("");
								address.setText("");
								phone.setText("");
								email.setText("");
								age.setText("");
								irum.requestFocus(); // 커서이동
								textArea.setText("수정되었습니다");
							}
						}
					
					else
						textArea.setText("데이터를 입력하지 않았습니다");
				}
				

			});
			button.setBounds(311, 278, 97, 23);
		}
		return button;
	}

	public void setD(ArrayList<Data6> list){
		this.list = list;

	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("나이");
			label.setForeground(new Color(255, 255, 255));
			label.setFont(new Font("굴림", Font.PLAIN, 11));
			label.setBounds(82, 282, 57, 15);
		}
		return label;
	}
	private JTextField getAge() {
		if (age == null) {
			age = new JTextField();
			age.setColumns(10);
			age.setBounds(165, 279, 116, 21);
		}
		return age;
	}
	private JLabel getLblModifyDate() {
		if (lblModifyDate == null) {
			lblModifyDate = new JLabel("            Modify Date");
			lblModifyDate.setOpaque(true);
			lblModifyDate.setForeground(Color.WHITE);
			lblModifyDate.setFont(new Font("Consolas", Font.PLAIN, 17));
			lblModifyDate.setBackground(Color.DARK_GRAY);
			lblModifyDate.setBounds(254, 20, 220, 21);
		}
		return lblModifyDate;
	}
}
